import com.badlogic.gdx.ApplicationAdapter
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.backends.lwjgl.LwjglApplication
import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.graphics.Texture
import com.badlogic.gdx.graphics.g2d.Animation
import com.badlogic.gdx.graphics.g2d.BitmapFont
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import com.badlogic.gdx.graphics.g2d.TextureRegion

fun main(args: Array<String>)
{
    LwjglApplication(MainPage(), config {
        width = 480
        height = 320
        useGL20 = true
        backgroundFPS = 30
        foregroundFPS = 120
        initialBackgroundColor = Color.GRAY
        resizable = false
        title = "AnimationDemo"
    })
}

class MainPage : ApplicationAdapter()
{

    val row by lazy { TextureRegion(Texture(Gdx.files.internal("row.jpg"))) }
    val keyFrames by lazy {
        val regions = row.split(512, 512)
        val result = com.badlogic.gdx.utils.Array<TextureRegion>()
        regions.forEach { array ->
            array.forEach {
                result.add(it)
            }
        }
        result
    }
    val animation by lazy { Animation(0.030f, keyFrames) }
    val batch by lazy { SpriteBatch() }
    var stateTime = 0f

    override fun create()
    {
        animation.playMode = Animation.NORMAL
    }

    override fun render()
    {
        clear()
        stateTime += Gdx.graphics.deltaTime

        batch.begin()

        batch.draw(animation.getKeyFrame(stateTime, true), 0f, 0f)

        batch.end()
    }

}